package com.hht.wblib;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.Uri;
import android.os.Environment;
import android.provider.Settings;
import android.util.Log;

import com.hht.wblib.bean.ConfigurationBean;
import com.hht.wblib.factory.WhiteBoardFactory;
import com.hht.wblib.utils.GsonHelper;
import com.hht.wblib.utils.IntentHelps;
import com.hht.wblib.utils.UniversalHelp;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

/**
 * Author:chen'hu
 * Time:2025/1/2 15:30
 * Description:This is WhiteBoardHelp
 */
public class WhiteBoardHelp {
    private Context mContext;

    private static WhiteBoardHelp INSTANCE;

    private boolean isExternalStorageManager = false;

    private static final String JSON_PATH = "/etc/Whiteboard/whiteboard.json";

    private WhiteBoardHelp(Context mContext) {
        this.mContext = mContext.getApplicationContext();
        initConfigurationJson();
    }

    private ConfigurationBean mConfigurationBean;

    private WhiteBoardFactory mWhiteBoardFactory;

    public static WhiteBoardHelp getInstance(Context context) {
        if (null == INSTANCE) {
            synchronized (WhiteBoardHelp.class) {
                if (null == INSTANCE) {
                    INSTANCE = new WhiteBoardHelp(context);
                }
            }
        }
        return INSTANCE;
    }

    /**
     *
     */
    @SuppressLint("NewApi")
    private void initConfigurationJson() {

        if (!Environment.isExternalStorageManager()) {
            IntentHelps.getInstance(mContext)
                    .setIntentConfigurator(intent -> {
                        intent.setAction(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
                        intent.setData(Uri.parse("package:" + mContext.getPackageName()));
                    })
                    .startActivity();
        } else {
            try {
                FileInputStream fileInputStream = new FileInputStream(JSON_PATH);
                InputStreamReader reader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8);
                StringBuilder jsonString = new StringBuilder();
                char[] buffer = new char[1024];
                int length;
                while ((length = reader.read(buffer)) != -1) {
                    jsonString.append(buffer, 0, length);
                }
                mConfigurationBean = GsonHelper.getInstance().stringToBean(jsonString.toString(), ConfigurationBean.class);
                isExternalStorageManager = true;
                Log.d("Hogan", "获取的机器配置数据为:" + mConfigurationBean.toString());
                fileInputStream.close();
                reader.close();
            } catch (Exception e) {
                isExternalStorageManager = false;
            }

            if (isExternalStorageManager) {
                mWhiteBoardFactory = new WhiteBoardFactory(mContext, mConfigurationBean);
            } else {
                mWhiteBoardFactory = null;
            }
        }

    }

    public boolean openAnnotation() {
        if (!isExternalStorageManager) {
            initConfigurationJson();
        }
        if (null == mWhiteBoardFactory) {
            Log.d("Hogan", "Failed to obtain the machine configuration. Enable the local method, openAnnotation");
            UniversalHelp.getInstance(mContext).openAnnotation();
            return false;
        } else {
            // Log.e("Hogan", "获取机器配置成功，openAnnotation" );
            return mWhiteBoardFactory.openAnnotation();
        }
    }


    public boolean openWhiteBoard() {

        if (!isExternalStorageManager) {
            initConfigurationJson();
        }
        if (null == mWhiteBoardFactory) {
            Log.d("Hogan", "Failed to obtain the machine configuration. Enable the local method, openWhiteBoard");
            return UniversalHelp.getInstance(mContext).openWhiteBoard();
        } else {
            // Log.e("Hogan", "获取机器配置成功，openWhiteBoard");
            return mWhiteBoardFactory.openWhiteBoard();
        }
    }

    public boolean sendPicturesToWhiteBoard(File file) {
        if (!isExternalStorageManager) {
            initConfigurationJson();
        }
        if (null == mWhiteBoardFactory) {
            Log.d("Hogan", "Failed to obtain the machine configuration. Enable the local method,sendPicturesToWhiteBoard");
            return UniversalHelp.getInstance(mContext).sendPicturesToWhiteBoard(file);
        } else {
            // Log.e("Hogan", "获取机器配置成功，sendPicturesToWhiteBoard");
            return mWhiteBoardFactory.sendPicturesToWhiteBoard(file);
        }
    }

}
